package com.ewhale.feitengguest.ui.user.mine;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.ewhale.feitengguest.R;
import com.ewhale.feitengguest.dto.AuthInfoDto;
import com.ewhale.feitengguest.presenter.user.mine.RNCertificationPresenter;
import com.ewhale.feitengguest.view.user.mine.RNCertificationView;
import com.simga.library.activity.MBaseActivity;
import com.simga.library.utils.CheckUtil;
import com.simga.library.widget.BGButton;

import butterknife.BindView;
import butterknife.OnClick;

public class RNCertificationActivity extends MBaseActivity<RNCertificationPresenter> implements RNCertificationView/**, EasyPermissions.PermissionCallbacks*/, TextWatcher {
    @BindView(R.id.tv_status)
    TextView mTvStatus;
    @BindView(R.id.et_realyName)
    EditText mEtRealyName;
    @BindView(R.id.et_IdCard)
    EditText mEtIdCard;
//    @BindView(R.id.iv_cardFront)
//    ImageView mIvCardFront;
//    @BindView(R.id.iv_cardRear)
//    ImageView mIvCardRear;
//    @BindView(R.id.iv_hand_cardFront)
//    ImageView mIvHandCardFront;
//    @BindView(R.id.iv_hand_cardRear)
//    ImageView mIvHandCardRear;
    @BindView(R.id.tv_commit)
    TextView mBtnCommit;
//    @BindView(R.id.iv_deleteCardFront)
//    ImageView mIvDeleteCardFront;
//    @BindView(R.id.iv_deleteCardRear)
//    ImageView mIvDeleteCardRear;
//    @BindView(R.id.iv_deleteHandCardFront)
//    ImageView mIvDeleteHandCardFront;
//    @BindView(R.id.iv_deleteHandCardRear)
//    ImageView mIvDeleteHandCardRear;
    @BindView(R.id.btn_commit_again)
    BGButton btnCommitAgain;
//    @BindView(R.id.cl_handCard)
//    ConstraintLayout mClHandCard;

//    private int chooseType = 1;
//    private static final int REQUEST_CODE_PERMISSION_2 = 10002;
//    private static final int REQUEST_CODE_CHOOSE_COMMENT_IMAGE = 1000;
//    private int uploadImage;
//    private String idCardFront;
//    private String idCardRear;
//    private String idHanCardFront;
//    private String idHanCardRear;
    private AuthInfoDto mAuthInfoDto;
//    private SelectPhotoDialog mPhotoDialog;

    public static void open(MBaseActivity context) {
        context.startActivity(null, RNCertificationActivity.class);
    }

    @Override
    protected int getViewId() {
        return R.layout.activity_rncertification;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        presenter.loadAuthInfo();
//        mPhotoDialog = new SelectPhotoDialog(mContext);
    }

    @Override
    protected boolean isUseBaseTitleBar() {
        return false;
    }

    @Override
    protected void initListener() {
        mBtnCommit.setOnClickListener(v -> {
            String name = mEtRealyName.getText().toString();
            String idCard = mEtIdCard.getText().toString();
            presenter.commitAuth(name, idCard/**, idCardFront, idCardRear, idHanCardFront, idHanCardRear*/);
        });
        btnCommitAgain.setOnClickListener(v -> {
            String name = mEtRealyName.getText().toString();
            String idCard = mEtIdCard.getText().toString();
//             || CheckUtil.isNull(idCardFront) || CheckUtil.isNull(idCardRear)
            if (CheckUtil.isNull(name) || CheckUtil.isNull(idCard)) {
                showToast("请先完善资料");
                return;
            }
//            if (mAuthInfoDto.getHandCertificateSetting() == 1) {
//                if (CheckUtil.isNull(idHanCardFront) || CheckUtil.isNull(idHanCardRear)) {
//                    showToast("请先完善资料");
//                    return;
//                }
//            }
            presenter.commitAuth(name, idCard/**, idCardFront, idCardRear, idHanCardFront, idHanCardRear*/);
        });
        mEtIdCard.addTextChangedListener(this);
        mEtRealyName.addTextChangedListener(this);
//        mPhotoDialog.setOnClickItem(new SelectPhotoDialog.onClickItem() {
//            @Override
//            public void onOneClick() {
//                chooseType = 1;
//                requestPermissionsMain();
//            }
//
//            @Override
//            public void onTwoClick() {
//                chooseType = 2;
//                requestPermissionsMain();
//            }
//        });
    }

    @Override
    public void showProLoading() {
        showLoading();
    }

    @Override
    public void dimissProLoading() {
        dismissLoading();
    }

    @Override
    public void showErrorMessage(int postCode, String code, String message) {
        showErrorMsg(code, message);
    }

    @OnClick({/**R.id.iv_cardFront, R.id.iv_cardRear, R.id.iv_hand_cardFront, R.id.iv_hand_cardRear, */R.id.iv_back})
    public void onViewClicked(View view) {
//        if (mAuthInfoDto.getStatus() == 0) {
//            return;
//        }
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
//            case R.id.iv_cardFront:
//                uploadImage = 1;
//                break;
//            case R.id.iv_cardRear:
//                uploadImage = 2;
//                break;
//            case R.id.iv_hand_cardFront:
//                uploadImage = 3;
//                break;
//            case R.id.iv_hand_cardRear:
//                uploadImage = 4;
//                break;
        }
//        mPhotoDialog.show();
    }

//    @AfterPermissionGranted(REQUEST_CODE_PERMISSION_2)
//    private void requestPermissionsMain() {
//        String[] perms = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
//                Manifest.permission.READ_EXTERNAL_STORAGE,
//                Manifest.permission.CAMERA};
//        if (EasyPermissions.hasPermissions(mContext, perms)) {
//            if (chooseType == 1) {
//                PictureUtils.openCameraRectangle(this, REQUEST_CODE_CHOOSE_COMMENT_IMAGE);
//            } else {
//                PictureUtils.openAluamOneRectangle(this, REQUEST_CODE_CHOOSE_COMMENT_IMAGE);
//            }
//        } else {
//            EasyPermissions.requestPermissions(this, "申请获取相关权限",
//                    REQUEST_CODE_PERMISSION_2, perms);
//        }
//    }
//
//    @Override
//    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
//        if (chooseType == 1) {
//            PictureUtils.openCameraRectangle(this, REQUEST_CODE_CHOOSE_COMMENT_IMAGE);
//        } else {
//            PictureUtils.openAluamOneRectangle(this, REQUEST_CODE_CHOOSE_COMMENT_IMAGE);
//        }
//    }
//
//    @Override
//    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
//        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
//            new AppSettingsDialog.Builder(this).setTitle("权限申请")
//                    .setRationale("申请获取相关权限")
//                    .setPositiveButton(getString(R.string.comfir))
//                    .setNegativeButton(getString(R.string.cancle))
//                    .build()
//                    .show();
//        }
//    }
//
//    @Override
//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
//    }
//
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (resultCode != RESULT_OK) {
//            return;
//        }
//        if (requestCode == REQUEST_CODE_CHOOSE_COMMENT_IMAGE) {
//            List<String> images = new ArrayList<>();
//            images.add(PictureSelector.obtainMultipleResult(data).get(0).getCutPath());
//            showLoading();
//            QiniuUtils.uploadImage(images, new QiniuUtils.onUploadImageCallBack() {
//                @Override
//                public void onResult(List<String> keys) {
//                    dismissLoading();
//                    if (uploadImage == 1) {
//                        idCardFront = keys.get(0);
//                        GlideUtil.loadPicture(idCardFront, mIvCardFront, R.mipmap.d1_btn_camera);
//                    } else if (uploadImage == 2) {
//                        idCardRear = keys.get(0);
//                        GlideUtil.loadPicture(idCardRear, mIvCardRear, R.mipmap.d1_btn_camera);
//                    } else if (uploadImage == 3) {
//                        idHanCardFront = keys.get(0);
//                        GlideUtil.loadPicture(idHanCardFront, mIvHandCardFront, R.mipmap.d1_btn_camera);
//                    } else if (uploadImage == 4) {
//                        idHanCardRear = keys.get(0);
//                        GlideUtil.loadPicture(idHanCardRear, mIvHandCardRear, R.mipmap.d1_btn_camera);
//                    }
//                    showDeleteImage();
//                    isEnableBtn();
//                    PictureUtils.clearCache(mContext);
//                }
//            });
//        }
//    }

    @Override
    public void showAuthInfo(AuthInfoDto infoDto) {
        mAuthInfoDto = infoDto;
        if (infoDto.getStatus() == -1) {
            mTvStatus.setVisibility(View.GONE);
            btnCommitAgain.setVisibility(View.INVISIBLE);
            mBtnCommit.setEnabled(false);
            mBtnCommit.setVisibility(View.VISIBLE);
        } else if (infoDto.getStatus() == 0) {
            mTvStatus.setVisibility(View.VISIBLE);
            btnCommitAgain.setVisibility(View.INVISIBLE);
            mTvStatus.setText("审核中");
            mTvStatus.setBackgroundColor(ContextCompat.getColor(mContext, R.color.main_color));
            mBtnCommit.setEnabled(false);
            mBtnCommit.setVisibility(View.GONE);
            mEtRealyName.setFocusable(false);
            mEtIdCard.setFocusable(false);
        } else if (infoDto.getStatus() == 1) {
            btnCommitAgain.setVisibility(View.VISIBLE);
            mTvStatus.setVisibility(View.VISIBLE);
            mTvStatus.setText("审核失败");
            mBtnCommit.setVisibility(View.GONE);
            mTvStatus.setBackgroundColor(Color.parseColor("#DDDDDD"));
            mBtnCommit.setEnabled(true);
        } else if (infoDto.getStatus() == 2) {
            btnCommitAgain.setVisibility(View.INVISIBLE);
            mTvStatus.setVisibility(View.VISIBLE);
            mTvStatus.setText("已认证");
            mTvStatus.setBackgroundColor(ContextCompat.getColor(mContext, R.color.main_color));
            mBtnCommit.setVisibility(View.GONE);
            mBtnCommit.setEnabled(true);
        }
//        if (infoDto.getHandCertificateSetting() == 0) {
//            mClHandCard.setVisibility(View.GONE);
//        } else {
//            mClHandCard.setVisibility(View.VISIBLE);
//        }
        mEtRealyName.setText(infoDto.getName());
        mEtIdCard.setText(infoDto.getCertificateNumber());
//        idCardFront = infoDto.getCertificateFrontImg();
//        GlideUtil.loadPicture(idCardFront, mIvCardFront, R.mipmap.d1_btn_camera);
//        idCardRear = infoDto.getCertificateBackImg();
//        GlideUtil.loadPicture(idCardRear, mIvCardRear, R.mipmap.d1_btn_camera);
//        idHanCardFront = infoDto.getHandFrontImg();
//        GlideUtil.loadPicture(idHanCardFront, mIvHandCardFront, R.mipmap.d1_btn_camera);
//        idHanCardRear = infoDto.getHandBacktImg();
//        GlideUtil.loadPicture(idHanCardRear, mIvHandCardRear, R.mipmap.d1_btn_camera);
//        showDeleteImage();
        isEnableBtn();
    }

//    private void showDeleteImage() {
//        if (CheckUtil.isNull(idCardFront)) {
//            mIvDeleteCardFront.setVisibility(View.GONE);
//        } else {
//            if (mAuthInfoDto.getStatus() == 0) {
//                mIvDeleteCardFront.setVisibility(View.GONE);
//            } else {
//                mIvDeleteCardFront.setVisibility(View.VISIBLE);
//            }
//        }
//        if (CheckUtil.isNull(idCardRear)) {
//            mIvDeleteCardRear.setVisibility(View.GONE);
//        } else {
//            if (mAuthInfoDto.getStatus() == 0) {
//                mIvDeleteCardRear.setVisibility(View.GONE);
//            } else {
//                mIvDeleteCardRear.setVisibility(View.VISIBLE);
//            }
//        }
//        if (CheckUtil.isNull(idHanCardFront)) {
//            mIvDeleteHandCardFront.setVisibility(View.GONE);
//        } else {
//            if (mAuthInfoDto.getStatus() == 0) {
//                mIvDeleteHandCardFront.setVisibility(View.GONE);
//            } else {
//                mIvDeleteHandCardFront.setVisibility(View.VISIBLE);
//            }
//        }
//        if (CheckUtil.isNull(idHanCardRear)) {
//            mIvDeleteHandCardRear.setVisibility(View.GONE);
//        } else {
//            if (mAuthInfoDto.getStatus() == 0) {
//                mIvDeleteHandCardRear.setVisibility(View.GONE);
//            } else {
//                mIvDeleteHandCardRear.setVisibility(View.VISIBLE);
//            }
//        }
//    }

    @Override
    public void commitAuth() {
        showToast("资料已提交审核");
        mTvStatus.setVisibility(View.VISIBLE);
        mTvStatus.setText("审核中");
        mTvStatus.setBackgroundColor(ContextCompat.getColor(mContext, R.color.main_color));
        mBtnCommit.setEnabled(false);
        mBtnCommit.setVisibility(View.GONE);
        btnCommitAgain.setVisibility(View.GONE);
    }

    private void isEnableBtn() {
        if (checkInputAllInfo()) {
            mBtnCommit.setEnabled(true);
        } else {
            mBtnCommit.setEnabled(false);
        }
    }

    /**
     * 判断是否有完善信息
     *
     * @return
     */
    private boolean checkInputAllInfo() {
        String name = mEtRealyName.getText().toString();
        String idCard = mEtIdCard.getText().toString();
        if (CheckUtil.isNull(name) || CheckUtil.isNull(idCard)
                /**|| CheckUtil.isNull(idCardFront) || CheckUtil.isNull(idCardRear)*/) {
            return false;
        }
        if (mAuthInfoDto.getHandCertificateSetting() == 0) {
            return true;
        }
//        else {
//            if (CheckUtil.isNull(idHanCardFront) || CheckUtil.isNull(idHanCardRear)) {
//                return false;
//            }
//        }
        return true;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        isEnableBtn();
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
//
//    @OnClick({R.id.iv_deleteCardFront, R.id.iv_deleteCardRear, R.id.iv_deleteHandCardFront, R.id.iv_deleteHandCardRear})
//    public void onViewClicked2(View view) {
//        switch (view.getId()) {
//            case R.id.iv_deleteCardFront:
//                idCardFront = "";
//                mIvCardFront.setImageResource(R.mipmap.d1_btn_camera);
//                break;
//            case R.id.iv_deleteCardRear:
//                idCardRear = "";
//                mIvCardRear.setImageResource(R.mipmap.d1_btn_camera);
//                break;
//            case R.id.iv_deleteHandCardFront:
//                idHanCardFront = "";
//                mIvHandCardFront.setImageResource(R.mipmap.d1_btn_camera);
//                break;
//            case R.id.iv_deleteHandCardRear:
//                idHanCardRear = "";
//                mIvHandCardRear.setImageResource(R.mipmap.d1_btn_camera);
//                break;
//        }
//        isEnableBtn();
//        showDeleteImage();
//    }
}
